package com.cneport.das.common.utils;

import com.cneport.das.common.model.param.ClientRequest;
import com.cneport.das.common.model.param.ClientResponse;
import com.cneport.das.common.model.trans.ClientGetRequest;
import com.cneport.das.common.model.trans.ClientPutRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc 报文加解密工具
 * @Ver 1.0
 * @Author GavinZhang
 * @Company 东方物通科技有限（北京）公司
 * @Date 2021-12-17 10:47:54
 */
public class EncryptUtil {


    public static ClientRequest encode(ClientRequest clientRequest, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        ClientRequest.QryReq qryReq = clientRequest.getQryReq();
        String code = qryReq.getParaInfo() == null ? "" : qryReq.getParaInfo();
        code = SecurityUtils.encode(keyPath, code, isPriKey);
        String type = qryReq.getParaType() == null ? "" : qryReq.getParaType();
        type = SecurityUtils.encode(keyPath, type, isPriKey);
        String lic = qryReq.getCopLic() == null ? "" : qryReq.getCopLic();
        lic = SecurityUtils.encode(keyPath, lic, isPriKey);

        qryReq.setParaInfo(code);
        qryReq.setParaType(type);
        qryReq.setCopLic(lic);
        clientRequest.setQryReq(qryReq);
        return clientRequest;
    }

    public static ClientRequest decode(ClientRequest clientRequest, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        ClientRequest.QryReq qryReq = clientRequest.getQryReq();
        String code = qryReq.getParaInfo() == null ? "" : qryReq.getParaInfo();
        code = SecurityUtils.decode(keyPath, code, isPriKey);
        String type = qryReq.getParaType() == null ? "" : qryReq.getParaType();
        type = SecurityUtils.decode(keyPath, type, isPriKey);
        String lic = qryReq.getCopLic() == null ? "" : qryReq.getCopLic();
        lic = SecurityUtils.decode(keyPath, lic, isPriKey);

        qryReq.setParaInfo(code);
        qryReq.setParaType(type);
        qryReq.setCopLic(lic);
        clientRequest.setQryReq(qryReq);
        return clientRequest;
    }

    public static ClientResponse encode(ClientResponse clientResponse, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        ClientResponse.QryRetn qryRetn = clientResponse.getQryRetn();
        String type = qryRetn.getParaType() == null ? "" : qryRetn.getParaType();
        type = SecurityUtils.encode(keyPath, type, isPriKey);
        String data = clientResponse.getBizData().getData() == null ? "" : clientResponse.getBizData().getData();
        data = SecurityUtils.encode(keyPath, data, isPriKey);

        qryRetn.setParaType(type);
        clientResponse.setQryRetn(qryRetn);
        clientResponse.getBizData().setData(data);

        return clientResponse;
    }

    public static ClientResponse decode(ClientResponse clientResponse, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        ClientResponse.QryRetn qryRetn = clientResponse.getQryRetn();
        String type = qryRetn.getParaType();
        type = SecurityUtils.decode(keyPath, type, isPriKey);
        String data = clientResponse.getBizData().getData();
        data = SecurityUtils.decode(keyPath, data, isPriKey);

        qryRetn.setParaType(type);
        clientResponse.setQryRetn(qryRetn);
        clientResponse.getBizData().setData(data);

        return clientResponse;
    }


    public static ClientPutRequest encode(ClientPutRequest clientPutRequest, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        String data = clientPutRequest.getReqData().getData() == null ? "" : clientPutRequest.getReqData().getData();
        String copLic = clientPutRequest.getReqHead().getCopLic() == null ? "" : clientPutRequest.getReqHead().getCopLic();
        data = SecurityUtils.encode(keyPath, data, isPriKey);
        copLic = SecurityUtils.encode(keyPath, copLic, isPriKey);
        clientPutRequest.getReqData().setData(data);
        clientPutRequest.getReqHead().setCopLic(copLic);
        return clientPutRequest;
    }

    public static ClientPutRequest decode(ClientPutRequest clientPutRequest, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        String data = clientPutRequest.getReqData().getData() == null ? "" : clientPutRequest.getReqData().getData();
        String copLic = clientPutRequest.getReqHead().getCopLic() == null ? "" : clientPutRequest.getReqHead().getCopLic();
        data = SecurityUtils.decode(keyPath, data, isPriKey);
        copLic = SecurityUtils.decode(keyPath, copLic, isPriKey);
        clientPutRequest.getReqData().setData(data);
        clientPutRequest.getReqHead().setCopLic(copLic);
        return clientPutRequest;
    }

    public static ClientGetRequest encode(ClientGetRequest clientPutRequest, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        String copLic = clientPutRequest.getReqHead().getCopLic() == null ? "" : clientPutRequest.getReqHead().getCopLic();
        copLic = SecurityUtils.encode(keyPath, copLic, isPriKey);
        clientPutRequest.getReqHead().setCopLic(copLic);
        return clientPutRequest;
    }

    public static ClientGetRequest decode(ClientGetRequest clientPutRequest, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        String copLic = clientPutRequest.getReqHead().getCopLic() == null ? "" : clientPutRequest.getReqHead().getCopLic();
        copLic = SecurityUtils.decode(keyPath, copLic, isPriKey);
        clientPutRequest.getReqHead().setCopLic(copLic);
        return clientPutRequest;
    }

    public static List<String> encode(List<String> data, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        List<String> encodes = new ArrayList<>();
        if (data != null && data.size() > 0) {

            int sz = data.size();
            for (int i = 0; i < sz; i++) {
                String d = data.get(i);
                d = SecurityUtils.encode(keyPath, d, isPriKey);
                encodes.add(d);
            }
        }
        return encodes;
    }

    public static List<String> decode(List<String> data, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        List<String> encodes = new ArrayList<>();
        if (data != null && data.size() > 0) {
            int sz = data.size();
            for (int i = 0; i < sz; i++) {
                String d = data.get(i);
                d = SecurityUtils.decode(keyPath, d, isPriKey);
                encodes.add(d);
            }
        }
        return encodes;
    }

    public static String encode(String data, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        if (StringUtil.isBlank(data)) {
            data = "";
        }
        data = SecurityUtils.encode(keyPath, data, isPriKey);
        return data;
    }

    public static String decode(String data, String keyPath, boolean isPriKey) {
        if (!keyPath.startsWith("file:")) {
            keyPath = "file:" + keyPath;
        }
        data = SecurityUtils.decode(keyPath, data, isPriKey);
        return data;
    }
}
